<?php
$page = 'add_app';
include 'connect/connect.php';
if (isset($_GET['save'])) {
    session_start();
    extract($_POST);
    $added_by = $_SESSION['username'];
    $query = "INSERT INTO apps SET app_name='$app_name', app_url='$app_url',  added_by='$added_by'";
    mysql_query($query) or die("Error executing '$query':" . mysql_error());
    header("Location: ?");
}

if (isset($_GET['update'])) {
    session_start();
    extract($_POST);
    $tid = $_GET['id'];
    $query = "UPDATE apps SET app_name='$app_name', app_url='$app_url'  WHERE id='$tid' ";
    mysql_query($query) or die("Error executing '$query':" . mysql_error());
    header("Location: ?");
}

include_once 'header.php';
?>
<h2 style="text-transform: capitalize">Welcome <?php echo $_SESSION['username']; ?></h2><br/>

<div id="edit_result"></div>
<div id="emulator"></div>
<div id="delete_result" style="display:none">
    <p class="error">App deleted!</p>
</div>
<script type="text/javascript">
    initDialog("#edit_result","Edit app");
    initDialog("#delete_result","App deleted");
            
    $(function() {
        $("#emulator").dialog({
            autoOpen: false,
            modal: false,
            title: "Emulator",
            width: 285,
            height:543
        });
    });

    jQuery(document).ready(function(){
        jQuery("#app_form").validationEngine();
    });

    function editApp(tid){
        $("#edit_result").hide();
        $("#loading").show();
        //        prompt("", "/iboard_more/"+action.toLowerCase()+"/{{tabName}}/"+tid);
        $("#edit_result").load("edit.php?id="+tid,function() {
            openDialog('#edit_result');
            $("#edit_result").show();
            $("#loading").hide();
        });
    }
            
    function emulate(tid){
        $("#emulator").hide();
        $("#loading").show();
        //        prompt("", "/iboard_more/"+action.toLowerCase()+"/{{tabName}}/"+tid);
        $("#emulator").load("emulator.php?appId="+tid,function() {
            openDialog('#emulator');
            $("#emulator").show();
            $("#loading").hide();
        });
    }
    
    
    function deleteApp(tid){
        if(confirm("Are you sure you want to delete?")){
            $("#loading").show();
            //        prompt("", "/iboard_more/"+action.toLowerCase()+"/{{tabName}}/"+tid);
            $("#edit_result").load("delete.php?id="+tid,function() {
                openDialog('#delete_result');
                $("#loading").hide();
            });
            window.location="?";
        }
    }
</script>
<img src="img/loading.gif" alt="Loading.." style="display: none;" id="loading"/>
<h3>Fill the form below to register a new application</h3>
<form action="?save=1" method="POST" id="app_form">
    <table>
        <tr><td>Application Name</td>
            <td><input type="text" class="validate[required] wide" name="app_name" id='app_name'/></td>
            <td>Home page Url</td>
            <td><input type="text" class="validate[required] wide" name="app_url" id='app_url' /></td>
            <td colspan="2" style="text-align: right;"><input type="submit"/></td>
        </tr>
    </table>
</form><br/>
<p>When you register an application, it becomes immediately visible to mobile phone users and they can access and use it. To register your application, first
host it somewhere online and then fill the form above. The application must be written using g-Apps API.</p>

<p>By registering your application (i.e. filling and submiting the above form, you agree to the following terms:</p>
<ol style="color: #000"><li>The application registered cannot harm the user's phone in  any way</li>
    <li>The application registered will not affect our servers negatively e.g. by causing denial of service or</li>
</ol>
<?php
$username = $_SESSION['username'];
$query = "SELECT * FROM apps where added_by='$username'";
$result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
if ($result == 0 ){
    echo "You have not uploaded any applications in W.A.R.E yet! Fill the form above to upload your first application.";
} else {
    ?>

<table style="width: 99%">
        <tr>
            <th>
                Application name
            </th>
            <th>
                Home page url
            </th>
            <th>
                Modify
            </th>
            <th>
                Usage
            </th>
        </tr>
        <?php
        while ($row = mysql_fetch_assoc($result)) {
            //var_dump($row);
            extract($row);
            ?>
            <tr>
                <td>
                    <?php
                    echo $app_name;
                    ?>
                </td>
                <td>
                    <?php
                    echo $app_url;
                    ?>
                </td>
                <td>
        <!--                            <a href="javascript:emulate(<?php echo $id ?>);">Emulate</a>-->
                    <a href="javascript:editApp(<?php echo $id ?>);"><img src="img/edit.png" alt="Edit" title="Edit"/></a>
                    <a href="javascript:deleteApp(<?php echo $id ?>);"><img src="img/delete.png" alt="Delete" title="Delete"/></a>
                </td>
                <td><a href="usage.php?appId=<?php echo $id ?>">Usage Statistics</a></td>

            </tr>
            <?php
        }
    }
    ?>
</table>


<?php include_once('footer.php'); ?>